<?php

namespace app\admin\services;

use app\admin\model\QuanGuanMemberModel;

class QuanGuanMemberService extends BaseService
{
    public function list($param = [], $field = "*")
    {
        $quanGuanMemberModel = QuanGuanMemberModel::makeInstance();
        $list = $quanGuanMemberModel->list($param, $field);
        return $list;
    }

    public function save($param = [], $id = 0)
    {
        $quanGuanMemberModel = QuanGuanMemberModel::makeInstance();
        if ($id == 0) {
            $param['create_time'] = time();
            $param['update_time'] = time();
            $param['create_id'] = session('aid');
            $param['update_id'] = session('aid');
            $res = $quanGuanMemberModel->mInsert($param);
        } else {
            $param['update_time'] = time();
            $param['update_id'] = session('aid');
            $res = $quanGuanMemberModel->mUpdate($param, $id);
        }
        return $res;
    }

    public function getInfoById($id, $param = [])
    {
        $quanGuanMemberModel = QuanGuanMemberModel::makeInstance();
        return $quanGuanMemberModel->getInfoById($id, $param);
    }

    /**
     * 获取有效数据
     */
    public function getOpenData($field = "*")
    {
        $quanGuanMemberModel = QuanGuanMemberModel::makeInstance();
        $list = $quanGuanMemberModel->getDatasByWhere([
            'is_delete' => 1,
            'status' => 1
        ], $field)->toArray();
        return array_column($list, null, 'id');
    }

    /**
     * 根据ids获取数据
     * @param array $ids
     * @param string $field
     * @return array
     */
    public function getDatasByIds($ids = [], $field = "*")
    {
        $quanGuanMemberModel = QuanGuanMemberModel::makeInstance();
        $list = $quanGuanMemberModel->getDatasByIds($ids, $field)->toArray();
        return array_column($list, null, 'id');
    }

    /**
     * 根据iden获取有效数据
     */
    public function getOpenDataByIden($field = "*", $iden = 1)
    {
        $quanGuanMemberModel = QuanGuanMemberModel::makeInstance();
        $list = $quanGuanMemberModel->where([
            'status' => 1,
            'is_delete' => 1,
            'identity' => $iden,
        ])->field($field)->select()->toArray();
        return array_column($list, null, 'id');
    }

    /**
     * 根据用户类型获取用户列表
     * @param int $identity
     * @param string $keyword
     * @param array $field
     */
    public function getMemberListByType($identity = 1, $keyword = "", $field = [])
    {
        $quanGuanMemberModel = QuanGuanMemberModel::makeInstance();
        if (isset($keyword) && !empty($keyword)) {
            $where[] = ['name', 'like', "%{$keyword}%"];
        }
        if (isset($identity) && $identity != 0) {
            $where[] = ['identity', '=', $identity];
        }
        $where[] = ['is_delete', 'eq', 1];
        $where[] = ['status', 'eq', 1];
        $list = $quanGuanMemberModel->where($where)->field($field)->select();
        return $list;
    }

}